package backgammon;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.util.ArrayList;

/**
 * <p>Represents a position on the game board.</p>
 * @author Jacob Kwitkoski
 * @version 1.0
 */
public abstract class Position {
    /**
     * <p>Maximum amount of pieces per position.</p>
     */
    private static final int PIECE_MAX = 15;

    /**
     * <p>Array list to hold the pieces on this pisition.</p>
     */
    protected ArrayList<Piece> pieces;

    protected ArrayList<Move> moves;

    protected Color color;

    protected final int arrayIndex;

    protected int drawAtX;

    protected Polygon polygon;

    /**
     * <p>Creates a new position with the specified color.</p>
     * @param positionColor the color of this position.
     */
    public Position(final Color newColor,
                            final int   index) {
        arrayIndex = index;
        color      = newColor;
        pieces     = new ArrayList(PIECE_MAX);
        moves      = new ArrayList();
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public Polygon getPolygon() {
        return polygon;
    }

    public void setPolygon(Polygon polygon) {
        this.polygon = polygon;
        drawAtX = polygon.xpoints[0];
    }

    /**
     * <p>Returns the list of pieces.</p>
     * @return the list of pieces.
     */
    public final ArrayList<Piece> getPieces() {
        return pieces;
    }

    public Piece removePiece() {
        return pieces.remove(pieces.size() - 1);
    }

    public final void addPiece(Piece p) {
        pieces.add(p);
    }

    /**
     * <p>Sets the new list of pieces.</p>
     * @param newPieces the new list of pieces.
     */
    public final void setPieces(final ArrayList<Piece> newPieces) {
        pieces = newPieces;
    }

    public abstract void drawPieces(Graphics g);

    public final void drawPosition(Graphics g) {
        g.setColor(color);
        g.fillPolygon(polygon);
    }

    public final boolean mouseIsIn(Point p) {
        boolean inside = false;

        if (polygon.contains(p)) {
            inside = true;
        }

        return inside;
    }

    public final boolean isBlottable() {
        boolean blottable = false;
        if (pieces.size() == 1) {
            blottable = true;
        }
        return blottable;
    }

    public boolean hasPieces() {
        boolean hasPieces = false;

        if (!pieces.isEmpty()) {
            hasPieces = true;
        }

        return hasPieces;
    }

    public int getArrayIndex() {
        return arrayIndex;
    }

    public Color getPieceColor() {
        return pieces.get(0).getColor();
    }

    public boolean hasMoves() {
        boolean hasMoves = false;
        if (moves.size() > 1) {
            hasMoves = true;
        }
        return hasMoves;
    }
    public ArrayList<Move> getMoves() {
        return moves;
    }

    public void setMoves(ArrayList<Move> newMoves) {
        moves = newMoves;
    }
}
